<template>
	<view class="forget">
		<cu-custom bgColor="bg-white" :isBack="true" >
			<block slot="backText"></block>
			<block slot="content">重置密码</block>
		</cu-custom>
		
		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">忘记了密码？可在此重置新密码。</view>
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="请输入手机号码"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="请输入新密码"
					isShowPass
				></wInput>
				
				<wInput
					v-model="verCode"
					type="text"
					maxlength="4"
					placeholder="验证码"
					
					isShowCode
					codeText="获取"
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
			
			<wButton 
				text="重置密码"
				:rotate="isRotate" 
				@click.native="startRePass()"
			></wButton>

		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../../components/watch-login/watch-input.vue'; //输入框
	import wButton from '../../../components/watch-login/watch-button.vue'; //按钮
	import {
		updatepassword
	} from '../../../service/api/login.js'
	export default {
		data() {
			return {
				phoneData: "", //电话
				passData: "", //密码
				verCode:"", //验证码
				isRotate: false, //是否加载旋转
				user:{
					telPhone:'',
					userPassword:'',
				},
			}
		},
		components:{
			wInput,
			wButton
		},
		mounted() {
			_this= this;
		},
		methods: {
			getVerCode(){
				//获取验证码
				if (this.phoneData.length != 11) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				console.log("获取验证码")
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
			},
			startRePass() {
				//重置密码
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length != 11) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
			    if (this.passData.length < 6) {
			        uni.showToast({
			            icon: 'none',
						position: 'bottom',
			            title: '密码不正确'
			        });
			        return false;
			    }
				if (this.verCode.length != 4) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				}				
				_this.isRotate=true
				_this.user.telPhone=_this.phoneData
				_this.user.userPassword=_this.passData
				//这里加了个延时，主要是为了让登录动画完整显示出来， 不需要可以删除
				var flag = true
				setTimeout(function(){
					updatepassword(_this.user)
					.then(res => {
						console.log(res) 
						//简单验证下登录（不安全）
						if(res.data.code==0){
							let userdata={
								"username":res.data.data.username,
								"userid":res.data.data.userId,
								"accesstoken":res.data.accesstoken,
							} //保存用户信息和accesstoken
							_this.$store.dispatch("setUserData",userdata); //存入状态
							try {
								uni.setStorageSync('setUserData', userdata); //存入缓存
							} catch (e) {
								// error
							}
							_this.isRotate=false
							uni.showToast({
								icon: 'success',
								position: 'bottom',
								title: '密码重置成功，自动登录ing---'
							});
							uni.reLaunch({
								url: '../../../pages/index',
							});
						}else{
							_this.isRotate=false
							_this.passData=""
							uni.showToast({
								icon: 'none',
								position: 'bottom',
								title: '密码更新失败'
							});
						}
						uni.hideLoading();
					}).catch(err => {
						console.log(err)
						let msg = err.errMsg; //错误信息
						uni.showToast({
						    icon: 'none',
							position: 'center',
						    title: '网络开小差了，小主稍后再试哦^.^'
						});
						_this.isRotate=false
						uni.hideLoading();
					})
					flag = false
				},2000)
				// 不能这么干，影响程序
				// while(flag){
				// 	console.log("循环中1234")
				// }
				console.log("重置密码成功1234")
			}
		},
	}
</script>

<style>
	@import url("../css/main.css");
</style>

